<?php

namespace Lib\PayApi\YeKaPay;
use Lib\Utils\Utils;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/22
 * Time: 1:36
 */
class PaymentClient {
    public static function pay($orderNo, $money, $pbank) {
        $banks = explode('_',$pbank);

        $yekapay = C('YEKAPAY');

        $p0_Cmd = $yekapay["cmd"];
        $p1_MerId = $yekapay["merchantId"];
        $p2_Order = $orderNo;
        $p3_Amt = $money;
        $p4_Cur = $yekapay["cur"];
        $p5_Pid = $yekapay["pid"];
        $p6_Pcat = $yekapay["pcat"];
        $p7_Pdesc = $yekapay["pdesc"];
        $p8_Url = $yekapay["notifyUrl"];
        $p9_SAF = $yekapay["SAF"];
        $pa_MP = "";
        $pd_FrpId = $yekapay['tongdao'][$banks[0]][$banks[1]];
        $pr_NeedResponse = $yekapay["needResponse"];
        $merchantKey = $yekapay["merchantKey"];
        $hmac = self::getReqHmacString($p0_Cmd, $p1_MerId, $p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url, $p9_SAF, $pa_MP,$pd_FrpId,$pr_NeedResponse, $merchantKey);

        $requestarray = array(
            "p0_Cmd" => $p0_Cmd,
            "p1_MerId" => $p1_MerId,
            "p2_Order" => $p2_Order,
            "p3_Amt" => $p3_Amt,
            "p4_Cur" => $p4_Cur,
            "p5_Pid" => $p5_Pid,
            "p6_Pcat" => $p6_Pcat,
            "p7_Pdesc" => $p7_Pdesc,
            "p8_Url" => $p8_Url,
            "p9_SAF" => $p9_SAF,
            "pa_MP" => $pa_MP,
            "pd_FrpId" => $pd_FrpId,
            "pr_NeedResponse" => $pr_NeedResponse,
            "hmac" => $hmac
        );
        // return Utils::post($yekapay['baseUri'], $requestarray);
        return $requestarray;
    }

    #签名函数生成签名串
    public static function getReqHmacString($p0_Cmd, $p1_MerId, $p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url, $p9_SAF, $pa_MP,$pd_FrpId,$pr_NeedResponse, $merchantKey) {
        #进行签名处理，一定按照文档中标明的签名顺序进行
        $sbOld = "";
        #加入业务类型
        $sbOld = $sbOld.$p0_Cmd;
        #加入商户编号
        $sbOld = $sbOld.$p1_MerId;
        #加入商户订单号
        $sbOld = $sbOld.$p2_Order;
        #加入支付金额
        $sbOld = $sbOld.$p3_Amt;
        #加入交易币种
        $sbOld = $sbOld.$p4_Cur;
        #加入商品名称
        $sbOld = $sbOld.$p5_Pid;
        #加入商品分类
        $sbOld = $sbOld.$p6_Pcat;
        #加入商品描述
        $sbOld = $sbOld.$p7_Pdesc;
        #加入商户接收支付成功数据的地址
        $sbOld = $sbOld.$p8_Url;
        #加入送货地址标识
        $sbOld = $sbOld.$p9_SAF;
        #加入商户扩展信息
        $sbOld = $sbOld.$pa_MP;
        #加入支付通道编码
        $sbOld = $sbOld.$pd_FrpId;
        #加入是否需要应答机制
        $sbOld = $sbOld.$pr_NeedResponse;
        self:: logstr($p2_Order,$sbOld,self::HmacMd5($sbOld,$merchantKey));
        return self::HmacMd5($sbOld,$merchantKey); // HmacMd5($sbOld,$merchantKey);
    }

    function getCallbackHmacString($r0_Cmd,$r1_Code,$r2_TrxId,$r3_Amt,$r4_Cur,$r5_Pid,$r6_Order,$r7_Uid,$r8_MP,$r9_BType) {
        $yekapay = C('YEKAPAY');

        #取得加密前的字符串
        $sbOld = "";
        #加入商家ID
        $sbOld = $sbOld.$yekapay["merchantId"];;
        #加入消息类型
        $sbOld = $sbOld.$r0_Cmd;
        #加入业务返回码
        $sbOld = $sbOld.$r1_Code;
        #加入交易ID
        $sbOld = $sbOld.$r2_TrxId;
        #加入交易金额
        $sbOld = $sbOld.$r3_Amt;
        #加入货币单位
        $sbOld = $sbOld.$r4_Cur;
        #加入产品Id
        $sbOld = $sbOld.$r5_Pid;
        #加入订单ID
        $sbOld = $sbOld.$r6_Order;
        #加入用户ID
        $sbOld = $sbOld.$r7_Uid;
        #加入商家扩展信息
        $sbOld = $sbOld.$r8_MP;
        #加入交易结果返回类型
        $sbOld = $sbOld.$r9_BType;

        // logstr($r6_Order,$sbOld,HmacMd5($sbOld,$merchantKey));
        return self::HmacMd5($sbOld,$yekapay["merchantKey"]);

    }

    public static  function CheckHmac($r0_Cmd,$r1_Code,$r2_TrxId,$r3_Amt,$r4_Cur,$r5_Pid,$r6_Order,$r7_Uid,$r8_MP,$r9_BType,$hmac) {
        if($hmac==self::getCallbackHmacString($r0_Cmd,$r1_Code,$r2_TrxId,$r3_Amt,$r4_Cur,$r5_Pid,$r6_Order,$r7_Uid,$r8_MP,$r9_BType))
            return true;
        else
            return false;
    }

    public static function HmacMd5($data,$key) {
        // RFC 2104 HMAC implementation for php.
        // Creates an md5 HMAC.
        // Eliminates the need to install mhash to compute a HMAC
        // Hacked by Lance Rushing(NOTE: Hacked means written)

        //需要配置环境支持iconv，否则中文参数不能正常处理
        $key = iconv("GB2312","UTF-8",$key);
        $data = iconv("GB2312","UTF-8",$data);

        $b = 64; // byte length for md5
        if (strlen($key) > $b) {
            $key = pack("H*",md5($key));
        }
        $key = str_pad($key, $b, chr(0x00));
        $ipad = str_pad('', $b, chr(0x36));
        $opad = str_pad('', $b, chr(0x5c));
        $k_ipad = $key ^ $ipad ;
        $k_opad = $key ^ $opad;

        return md5($k_opad . pack("H*",md5($k_ipad . $data)));
    }

    public static function logstr($orderid,$str,$hmac) {
        $logName = "BANK_HTML.log";
        $james=fopen($logName,"a+");
        fwrite($james,"\r\n".date("Y-m-d H:i:s")."|orderid[".$orderid."]|str[".$str."]|hmac[".$hmac."]");
        fclose($james);
    }
}